From bbd9571e5f8a9931c645148d96decf775c26603a Mon Sep 17 00:00:00 2001 From: "emellor@leeni.uk.xensource.com" Date: Mon, 21 Nov 2005 13:00:36 +0100 Subject: [PATCH] Added reset method to Opts so that create.parseCommandLine can be used more than once. This makes it possible to unit test this code. Remove the odd aliasing of gopts to opts inside parseCommandLine. Rename local variables globals and locals inside opts.load -- these are built-in functions. Signed-off-by: Ewan Mellor --- tools/python/xen/xm/create.py | 34 +++++++++++++++++----------------- tools/python/xen/xm/opts.py | 30 +++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/tools/python/xen/xm/create.py b/tools/python/xen/xm/create.py index 23ea2ba975..49a95ba277 100644 --- a/tools/python/xen/xm/create.py +++ b/tools/python/xen/xm/create.py @@ -879,33 +879,33 @@ def balloon_out(dom0_min_mem, opts): def parseCommandLine(argv): - opts = gopts - args = opts.parse(argv) - if opts.vals.help: - opts.usage() - if opts.vals.help or opts.vals.help_config: - opts.load_defconfig(help=1) - if opts.vals.help or opts.vals.help_config: + gopts.reset() + args = gopts.parse(argv) + if gopts.vals.help: + gopts.usage() + if gopts.vals.help or gopts.vals.help_config: + gopts.load_defconfig(help=1) + if gopts.vals.help or gopts.vals.help_config: return (None, None) - if not opts.vals.display: - opts.vals.display = os.getenv("DISPLAY") + if not gopts.vals.display: + gopts.vals.display = os.getenv("DISPLAY") # Process remaining args as config variables. for arg in args: if '=' in arg: (var, val) = arg.strip().split('=', 1) gopts.setvar(var.strip(), val.strip()) - if opts.vals.config: - config = opts.vals.config + if gopts.vals.config: + config = gopts.vals.config else: - opts.load_defconfig() - preprocess(opts.vals) - if not opts.getopt('name') and opts.getopt('defconfig'): - opts.setopt('name', os.path.basename(opts.getopt('defconfig'))) - config = make_config(opts.vals) + gopts.load_defconfig() + preprocess(gopts.vals) + if not gopts.getopt('name') and gopts.getopt('defconfig'): + gopts.setopt('name', os.path.basename(gopts.getopt('defconfig'))) + config = make_config(gopts.vals) - return (opts, config) + return (gopts, config) def main(argv): diff --git a/tools/python/xen/xm/opts.py b/tools/python/xen/xm/opts.py index 321e6783da..3f983407fc 100644 --- a/tools/python/xen/xm/opts.py +++ b/tools/python/xen/xm/opts.py @@ -60,6 +60,14 @@ class Opt: self.value = None self.set(default) + + def reset(self): + self.specified_opt = None + self.specified_val = None + self.value = None + self.set(self.default) + + def __repr__(self): return self.name + '=' + str(self.specified_val) @@ -223,6 +231,14 @@ class Opts: # Option to use for bare words. self.default_opt = None + + def reset(self): + self.vals = OptVals() + self.vars = {} + for opt in self.options: + opt.reset() + + def __repr__(self): return '\n'.join(map(str, self.options)) @@ -416,22 +432,22 @@ class Opts: are used to set options with the same names. Variables are not used to set options that are already specified. """ - # Create global and lobal dicts for the file. + # Create global and local dicts for the file. # Initialize locals to the vars. # Use exec to do the standard imports and # define variables we are passing to the script. - globals = {} - locals = {} - locals.update(self.vars) + globs = {} + locs = {} + locs.update(self.vars) cmd = '\n'.join(self.imports + [ "from xen.xm.help import Vars", "xm_file = '%s'" % defconfig, "xm_help = %d" % help, "xm_vars = Vars(xm_file, xm_help, locals())" ]) - exec cmd in globals, locals + exec cmd in globs, locs try: - execfile(defconfig, globals, locals) + execfile(defconfig, globs, locs) except: if not help: raise if help: @@ -444,7 +460,7 @@ class Opts: types.IntType, types.FloatType ] - for (k, v) in locals.items(): + for (k, v) in locs.items(): if self.specified(k): continue if not(type(v) in vtypes): continue self.setopt(k, v) -- 2.30.2